package zhaolu.com.example.Delicacy.Fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;

import zhaolu.com.example.Delicacy.Activity.OrderDetailActivity;
import zhaolu.com.example.Delicacy.Entity.ItemBean;
import zhaolu.com.example.Delicacy.MySQL.DishDAO;
import zhaolu.com.example.Delicacy.R;
import zhaolu.com.example.Delicacy.Utils.MyAdapter;

public class OrderFragment2 extends Fragment {
    private ListView listView;
    private List<ItemBean> beanList;
    private MyAdapter myAdapter;
    private DishDAO dishDAO;
    private int[] image = {
            R.drawable.dish1, R.drawable.dish2,
            R.drawable.dish3, R.drawable.dish4,
            R.drawable.dish5, R.drawable.dish6,
            R.drawable.dish7, R.drawable.dish8,
            R.drawable.dish9, R.drawable.dish10,
            R.drawable.dish11, R.drawable.dish12,
            R.drawable.dish13
    };
    private Button add;

    @Nullable
    @Override
    public View onCreateView(@Nullable LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.order_fragment, container, false);
        listView = view.findViewById(R.id.orderList);
        dishDAO = new DishDAO();
        beanList = new ArrayList<>();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                beanList = dishDAO.getDishBeanList();
            }
        });
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < beanList.size(); i++) {
            beanList.get(i).setImage(image[beanList.get(i).getIdFood() - 1]);
            String price = beanList.get(i).getDescription();
            beanList.get(i).setDescription("\n单价：" + price + "元");
        }
        SharedPreferences sp = getActivity().getSharedPreferences("login", Context.MODE_PRIVATE);
        String phone = sp.getString("username", null);
        myAdapter = new MyAdapter(getContext(), beanList, phone);
        listView.setAdapter(myAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ItemBean itemBean = beanList.get(position);
                String title = itemBean.getTitle();
                Intent intent = new Intent(getActivity(), OrderDetailActivity.class);
                intent.putExtra("item", itemBean);
                startActivity(intent);
            }
        });

        return view;
    }
}
